package com.ala4.oxcafe.service.impl;

import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.MenuInfo;
import com.ala4.oxcafe.exception.BusinessException;
import com.ala4.oxcafe.repository.MenuInfoRepository;
import com.ala4.oxcafe.service.MenuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单信息
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/8/12 17:08
 */
@Service
@AllArgsConstructor
public class MenuInfoServiceImpl implements MenuInfoService {

    private final MenuInfoRepository menuInfoRepository;

    @Override
    public List<MenuInfo> menuInfoList(MenuInfo menuInfo) {
        LambdaQueryWrapper<MenuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(menuInfo.getMenuName()), MenuInfo::getMenuName, menuInfo.getMenuName());
        queryWrapper.eq(StrUtil.isNotEmpty(menuInfo.getMenuType()), MenuInfo::getMenuType, menuInfo.getMenuType());
        queryWrapper.eq(ObjectUtil.isNotNull(menuInfo.getStatus()), MenuInfo::getStatus, menuInfo.getStatus());
        return menuInfoRepository.list(queryWrapper);
    }

    @Override
    public MenuInfo createMenuInfo(MenuInfo menuInfo) {
        // 这里的ID需要特殊处理一下 使用父节点增加的方式
        MenuInfo parentMenu = menuInfoRepository.getById(menuInfo.getParentId());
        if (ObjectUtil.isNull(parentMenu)) {
            throw new BusinessException("父节点不存在");
        }
        // 默认就是父节点后面增加一个001 每一级999够用了吧？
        String currentId = (ObjectUtil.isNull(menuInfo.getParentId()) ? menuInfo.getId() : menuInfo.getParentId()) + "001";

        // 查询父节点的子节点的最大的ID 这里应该不需要拼接逻辑删除
        Long maxId = menuInfoRepository.getBaseMapper().selectMaxIdMenu(menuInfo.getParentId());
        // 如果不为null 就是最大的id增加1
        if (ObjectUtil.isNotNull(maxId)) {
            currentId = String.valueOf(maxId + 1);
        }
        menuInfo.setId(Long.valueOf(currentId));
        menuInfoRepository.save(menuInfo);
        return menuInfo;
    }

    @Override
    public MenuInfo updateMenuInfo(MenuInfo menuInfo) {
        boolean b = menuInfoRepository.updateById(menuInfo);
        if (!b) {
            throw new BusinessException("更新失败");
        }
        return menuInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenuInfo(Long menuId) {
        MenuInfo menuInfo = menuInfoRepository.getById(menuId);
        if (ObjectUtil.isNull(menuInfo)) {
            throw new BusinessException("找不到菜单信息");
        }
        if (ObjectUtil.isNull(menuInfo.getParentId())) {
            throw new BusinessException("顶级菜单不能删除");
        }
        // 删除菜单
        menuInfoRepository.removeById(menuId);
        // 子菜单也要删除
        menuInfoRepository.deleteChildNode(menuId);
    }

    @Override
    public MenuInfo menuInfo(Long menuId) {
        return menuInfoRepository.getById(menuId);
    }
}
